<template>
	<div class="category-container">
		<table-components :tableLoading="tableLoading" :tableData="tableData" @currentChange="currentChange">
			<el-table-column prop="name" label="协议名称" align="center" />
			<el-table-column prop="version" label="版本号" align="center" />
			<el-table-column label="协议状态" align="center">
				<template v-slot="scope">
					<el-tag type="primary" v-if="scope.row.status == 0">启用</el-tag>
					<el-tag type="danger" v-if="scope.row.status == 1">禁用</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="createBy" label="创建人" align="center" />
			<el-table-column prop="createTime" label="创建时间" align="center" />
			<el-table-column label="操作" align="center">
				<template v-slot="scope">
					<el-button type="text" @click="openDetail(scope.row)">预览</el-button>
				</template>
			</el-table-column>
		</table-components>
	</div>

	<el-dialog v-model="dialogVisible1" title="协议预览" width="45%" :before-close="dialogClose" destroy-on-close>
		<div>
			<div v-html="clickItem.content"></div>
		</div>
		<template #footer>
			<span class="dialog-footer">
				<el-button type="primary" @click="submit">选择</el-button>
				<el-button @click="dialogClose">关闭</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { NextLoading } from '/@/utils/loading';
import TableComponents from '/@/components/tableComponents/index.vue';
import { getagreementsaasInfo, addSiteAgreement } from '/@/api/api';
import addDialog from './dialog/addDialog.vue';

const tableData = reactive({
	data: [],
	total: 0,
	page: 1,
	size: 20,
});
const tableLoading = ref(true);
const queryFrom = ref({});
const clickItem = ref();
const dialogVisible1 = ref(false);

const props = defineProps({
	dataType: {
		type: Number,
		default: () => 0,
	},
	role: {
		type: String,
		default: () => '',
	},
	platType: {
		type: Number,
		default: () => 0,
	},
});

// 页面加载时
onMounted(() => {
	NextLoading.done();
	getData();
});

async function getData() {
	tableLoading.value = true;
	let par = {
		dataType: props.dataType, //0：用户协议；1：隐私政策
		platType: props.platType,
		...queryFrom.value,
	};

	let { code, data } = await getagreementsaasInfo(par);
	if (code == 200) {
		tableData.data = data.list;
		tableData.total = data.total;
		tableLoading.value = false;
	}
}

function openDetail(item) {
	clickItem.value = item;
	dialogVisible1.value = true;
}

function dialogClose() {
	dialogVisible1.value = false;
}
const emit = defineEmits(['submit']);
async function submit() {
	emit('submit', clickItem.value);
}
</script>

<style scoped lang="scss">
@import url('/@/theme/page/page.scss');
.category-container {
	padding: 0 20px 20px 20px;
}
</style>
